package controller;

import java.util.PriorityQueue;

import simulator.enums.Direction;
import simulator.enums.LaneType;

import controller.trafficlights.LightPriorityComparer;
import controller.trafficlights.TrafficLight;

public class TrafficLightPriorityQueue extends PriorityQueue<TrafficLight>
{	
	public TrafficLightPriorityQueue(int initialSize, LightPriorityComparer comparator)
	{
		super(initialSize, comparator);
	}
	
	
	@Override
	public boolean add(TrafficLight light) 
	{
		setPriority(light);
		return super.add(light);
	}

	private void setPriority(TrafficLight light)
	{
		LaneType type = light.getLaneType();
		Direction dir = light.getDirection();
		int priority = 0;
		
		if(dir == Direction.NORTH || dir == Direction.SOUTH)
		{
			if(type == LaneType.BUSLANE)
			{
				priority += VehiclePriority.BUS.getValue();
				priority += LanePriority.NORTH_SOUTH_BUS.getValue();
			}
			if(type == LaneType.CAR_LEFT)
			{
				priority += VehiclePriority.CAR.getValue();
				priority += LanePriority.NORHT_SOUTH_LEFT.getValue();
			}
			if(type == LaneType.CAR_RIGHT)
			{
				priority +=	VehiclePriority.CAR.getValue();
				priority += LanePriority.NORHT_SOUTH_RIGHT.getValue();
			}
			if(type == LaneType.CAR_STRAIGHT)
			{
				priority += VehiclePriority.CAR.getValue();
				priority += LanePriority.NORTH_SOUTH_STRAIGHT.getValue();
			}
			if(type == LaneType.BIKELANE)
			{
				priority += VehiclePriority.BICYCLE.getValue();
				priority += LanePriority.NORTH_SOUTH_STRAIGHT.getValue();
			}
			if(type == LaneType.SIDEWALK_LEFTSIDE || type == LaneType.SIDEWALK_RIGHTSIDE)
			{
				priority += VehiclePriority.PEDESTRIAN.getValue();
				priority += LanePriority.NORTH_SOUTH_STRAIGHT.getValue();
			}
		}
		if(dir == Direction.EAST || dir == Direction.WEST)
		{
			if(type == LaneType.BUSLANE)
			{
				priority += VehiclePriority.BUS.getValue();
				priority += LanePriority.EAST_WEST_BUS.getValue();
			}
			if(type == LaneType.CAR_LEFT)
			{
				priority += VehiclePriority.CAR.getValue();
				priority += LanePriority.EAST_WEST_LEFT.getValue();
			}
			if(type == LaneType.CAR_RIGHT)
			{
				priority +=	VehiclePriority.CAR.getValue();
				priority += LanePriority.EAST_WEST_RIGHT.getValue();
			}
			if(type == LaneType.CAR_STRAIGHT)
			{
				priority += VehiclePriority.CAR.getValue();
				priority += LanePriority.EAST_WEST_STRAIGHT.getValue();
			}
			if(type == LaneType.BIKELANE)
			{
				priority += VehiclePriority.BICYCLE.getValue();
				priority += LanePriority.EAST_WEST_STRAIGHT.getValue();
			}
			if(type == LaneType.SIDEWALK_LEFTSIDE || type == LaneType.SIDEWALK_RIGHTSIDE)
			{
				priority += VehiclePriority.PEDESTRIAN.getValue();
				priority += LanePriority.EAST_WEST_STRAIGHT.getValue();
			}
		}
		light.setPriority(priority);
	}
}
